<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="24">
            <a-form-item label="第几周" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['weekProject', validatorRules.weekProject]" placeholder="请输入第几周"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择开始时间" v-decorator="['startTime', validatorRules.startTime]" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择结束时间" v-decorator="['endTime', validatorRules.endTime]" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>

      <hf-project-select-modal ref="hfProjectSelectModal" :multiple="false" @hf-bussiness-project-select="hfPmProjectSelect"/>

    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import HfProjectSelectModal from './HfProjectSelectModal'

  export default {
    name: "HfBussinessDoubleWeekPlanModal",
    components: {
      HfProjectSelectModal
    },
    data() {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
          // projectShortName: {
          //   rules: [
          //     { required: true, message: '请选择项目!'},
          //   ]
          // },
          weekProject: {
            rules: [
              { required: true, message: '请输入第几周!'},
            ]
          },
          startTime: {
            rules: [
              { required: true, message: '请选择计划开始时间!'},
            ]
          },
          endTime: {
            rules: [
              { required: true, message: '请输入计划结束时间!'},
            ]
          },
        },
        url: {
          add: "/bussiness/hfBussinessDoubleWeekPlan/add",
          edit: "/bussiness/hfBussinessDoubleWeekPlan/edit",
        }

      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'deleteFlag','projectId','projectShortName','weekProject','subjectId','targetId','planType','startTime','endTime'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      validateError(msg){
        this.$message.error(msg)
      },
      handleCancel () {
        this.close()
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'deleteFlag','projectId','projectShortName','weekProject','subjectId','targetId','planType','startTime','endTime'))
     },
      projectShortNameClick() {
        this.$nextTick(() => {
          this.$refs.hfProjectSelectModal.title = '选择项目'
          this.$refs.hfProjectSelectModal.visible = true
        })
      },
      hfPmProjectSelect(data) {
        this.form.setFieldsValue({
          projectId: data.id,
          projectShortName: data.shortName
        })
      },
    }
  }
</script>